package com.example.mq.mqserver.mapper;

import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.MSGQueue;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MetaMapper {
    //建表操作
    @Update("create table if not exists exchange(" +
            "name varchar(50) primary key," +
            "type int," +
            "durable boolean," +
            "autoDelete boolean," +
            "arguments varchar(1024))")
    public void createExchangeTable();
    @Update("create table if not exists queue(" +
            "name varchar(50) primary key," +
            "exclusive boolean," +
            "durable boolean," +
            "autoDelete boolean," +
            "arguments varchar(1024))")
    public void createQueueTable();
    @Update("create table if not exists binding(" +
            "exchangeName varchar(50)," +
            "queueName varchar(50)," +
            "bindingKey varchar(256))")
    public void createBindingTable();

    //基础添加、删除、查询操作
    @Insert("insert into exchange values(#{name}, #{type}, #{durable}, #{autoDelete}, #{arguments})")
    public void insertExchange(Exchange exchange);

    @Delete("delete from exchange where name = #{exchangeName}")
    public void deleteExchange(String exchangeName);

    @Select("select * from exchange")
    public List<Exchange> selectAllExchanges();

    @Insert("insert into queue values(#{name}, #{exclusive}, #{durable}, #{autoDelete}, #{arguments})")
    public void insertQueue(MSGQueue msgQueue);

    @Delete("delete from queue where name = #{queueName}")
    public void deleteQueue(String queueName);

    @Select("select * from queue")
    public List<MSGQueue> selectAllQueues();

    @Insert("insert into binding values(#{exchangeName}, #{queueName}, #{bindingKey})")
    public void insertBinding(Binding binding);

    @Delete("delete from binding where exchangeName = #{exchangeName} and queueName = #{queueName}")
    public void deleteBinding(String exchangeName, String queueName);

    @Select("select * from binding")
    public List<Binding> selectAllBindings();
}
